package com.softcits.email.common;

import java.io.File;
import java.util.Comparator;

public class FileComparator implements Comparator<File> {
    
    public FileComparator() {
        
    }
    
    
    public FileComparator(String columnName, String dir) {
        this.columnName = columnName;
        this.dir = dir;
    }
    
    private String columnName = "";
    
    private String dir;

    @Override
    public int compare(File f1, File f2) {
        
        
        int cres = compareTo(f1, f2);
        int result;
        if("desc".equalsIgnoreCase(dir)) {
            result = -cres;
        } else if("asc".equalsIgnoreCase(dir)) {
            result = cres;
        } else {
            result = 0;
        }
        
        return result;
    }
    
    public int compareTo(File f1, File f2) {
        switch (columnName) {
        case "modificationTime":
            long md1 = f1.lastModified();
            long md2 = f2.lastModified();
            return (int) (md1 - md2);
        case "size":
            long s1 = f1.length();
            long s2 = f2.length();
            
            return (int) (s1 - s2);

        default:
            break;
        }
        
        return 0;
    }

}
